﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace LoginForm
{
    public partial class pNoteDialog : Form
    {
        private string id;        
        public pNoteDialog(string strid)
        {
            InitializeComponent();
            this.id = strid;
            Pupil p = GetPupil();
            this.Text = "Note - " + p.Realname;
            ReadFile();
        }
        private Pupil GetPupil() {
            int i = Convert.ToInt32(id);
            Pupil p = SunnyIO.GetPupilById(FileName.pupil, i);
            return p;
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            Stream stream;
            OpenFileDialog openDialog = new OpenFileDialog();
            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                stream = openDialog.OpenFile();
                if (stream != null)
                {
                    string filePath = openDialog.FileName;
                    try
                    {
                        string pathConn = "Provider =  Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
                        OleDbConnection conn = new OleDbConnection(pathConn);
                        OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [Sheet2$A1:R26]", conn);  // "Select *from [" + text(Sheet1) +"] " or [Sheet1$A1:D12]
                        DataTable table = new DataTable();
                        adapter.Fill(table);
                        dataGridView1.DataSource = table;
                        
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        private void newToolStripButton1_Click(object sender, EventArgs e)
        {
            txtrContent.Text = "";
        }

        private void openToolStripButton1_Click(object sender, EventArgs e)
        {
            Stream stream;
            OpenFileDialog openDialog = new OpenFileDialog();
            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                stream = openDialog.OpenFile();
                if (stream != null) {
                    string filePath = openDialog.FileName;
                    try
                    {
                        string content = File.ReadAllText(filePath);
                        txtrContent.Text = content;
                    }
                    catch (Exception ex)
                    {
                        
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }
        private void ReadFile() {
            Pupil p = GetPupil();
            string filename = p.Nickname + id + ".txt";            
            if (!File.Exists(filename))
            {                
                StreamWriter sw = new StreamWriter(filename);
                sw.Close();
                txtrContent.Text = "";
            }
            else {
                StreamReader reader = new StreamReader(filename);
                string content = reader.ReadToEnd();
                txtrContent.Text = content;
                reader.Close();
            }
        }

        private void saveToolStripButton1_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Em Có Muốn Lưu Lại Ghi Chú Này ?", "Message Box",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Pupil p = GetPupil();
                string filename = p.Nickname + id + ".txt";
                //StreamWriter writer;
                //if (!File.Exists(filename))
                //{
                //    writer = new StreamWriter(filename);
                //}
                //else {
                //    writer = File.AppendText(filename);
                //}
                //writer.WriteLine(DateTime.Now);
                //writer.Write(txtrContent.Text);
                //writer.Close();
                FileStream file = File.Open(filename, FileMode.Create);
                using (StreamWriter sw = new StreamWriter(file))
                {
                    sw.Write(txtrContent.Text);
                    sw.WriteLine("\n Time Note: " + DateTime.Now);
                    MessageBox.Show("Lưu Ghi Chú Thành Công", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                this.Dispose();
            }
        }
        
    }
   
}
